#ifndef CAMERA_H
#define CAMERA_H

#include "Framework.h"
#include <vector>
#include "Enemy.h"

class Camera {
public:
	Camera() {
		x = -8.0;
		y = 30.0;
		//y = 1;
		z = 300.0;
		lx = 1.0;
		ly = -0.3;
		lz = 0.0;
		angleX = 0.0;
		angleY = 0.0;
		rotateX = 0.0;
		rotateY = 0.0;
		rx = 0.0;
		ry = 0.0;
		rz = 1.0;
		ux = 0.0;
		uy = 1.0;
		uz = 0.0;
		pushBack = 70;
		//pushBack = 4;
		followers = new std::vector<Enemy *>();
		idle = true;
	}

	void move(GLdouble d) {
		x = x + lx * d;
		//y = y + ly * d;
		z = z + lz * d;	
	}
	
	void strafe(GLdouble d) {
		x = x + rx * d;					
		//y = y + ry * d;
		z = z + rz * d;	
	}

	void rotateXDegrees(GLdouble r) {
		//if (rotateX + r > -89 && rotateX + r < 89) {
			angleX = 0;
			angleX += r;
			rotateX += r;
			updateAfterRotateX();
 		//} else {
		//	printf("rotateY=%f\n", rotateX);		
		//}
	}

	void rotateYDegrees(GLdouble r) {
		//if (rotateY + r > -89 && rotateY + r < 89) {
			angleY = 0;
			angleY += r;
			rotateY += r;
			updateAfterRotateY();
		//} else {
		//	printf("rotateY=%f\n", rotateY);		
		//}
	}

	void addFollower(Enemy *enemy) {
		followers->push_back(enemy);	
	}

	unsigned int getFollowersSize() {
		return followers->size();	
	}

	Enemy* getFollower(unsigned int index) {
		return followers->at(index);
	}

	GLdouble x;
	GLdouble lx;
	GLdouble y;
	GLdouble ly;
	GLdouble z;
	GLdouble lz;
	GLdouble angleX;
	GLdouble angleY;
	GLdouble rotateX;
	GLdouble rotateY;
	GLdouble rx;
	GLdouble ry;
	GLdouble rz;
	GLdouble ux;
	GLdouble uy;
	GLdouble uz;
	GLdouble pushBack;
	bool idle;
private:
	void updateAfterRotateX() {
		rx = rx * cos(angleX * M_PI/180.0) + lx * sin(angleX * M_PI/180.0);
		//ry = ry * cos(angleX * M_PI/180.0) + ly * sin(angleX * M_PI/180.0);
		rz = rz * cos(angleX * M_PI/180.0) + lz * sin(angleX * M_PI/180.0);

		GLdouble l = sqrt(rx * rx + ry * ry + rz * rz);
		rx = rx / l;
		//ry = ry / l;
		rz = rz / l;
				
		lx = (ry * uz - rz * uy) * -1;
		//ly = (rz * ux - rx * uz) * -1;
		lz = (rx * uy - ry * ux) * -1;	
	}

	void updateAfterRotateY() {
		lx = lx * cos(angleY * M_PI/180.0) + ux * sin(angleY * M_PI/180.0);
		ly = ly * cos(angleY * M_PI/180.0) + uy * sin(angleY * M_PI/180.0);
		lz = lz * cos(angleY * M_PI/180.0) + uz * sin(angleY * M_PI/180.0);

		GLdouble l = sqrt(lx * lx + ly * ly + lz * lz);
		lx = lx / l;
		ly = ly / l;
		lz = lz / l;

		ux = (ly * rz - lz * ry) * -1;
		uy = (lz * rx - lx * rz) * -1;
		uz = (lx * ry - ly * rx) * -1;
	}
	std::vector<Enemy *> *followers;
	
};

#endif
